<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db;
use cc\storage;
use video\Conf;
use think\Cache;
use video\VodApi;
use think\Request;
use cc\spark_function;
use app\atxunda\model\KnowUser;
use app\index_new\model\CourseExperience;
use app\atxunda\model\Course as CourseModel;
use video\qcloudapi\src\QcloudApi\QcloudApi;



class Course extends Common
{

	/**
	 * [index 具体课程展示]
	 * @return [type] [description]
	 */
	public function index(){
		$request = Request::instance();
		$admin = unserialize(cookie('admin'));
		$wherenav = $where = array();
		$datas = $request->param();
		if($request->param()){
			if(empty($datas)){
				$datas['status'] = 'select';
			}
			$arr = CourseModel::disposeData($datas);
			$url = '?status=select&page='.$datas['page'];
			if(input('post.nav_id')) $url .= '&nav_id='.input('post.nav_id');
			if(input('post.type_id')) $url .= '&type_id='.input('post.type_id');
			if(input('post.s_id')) $url .= '&s_id='.input('post.s_id');
			if(!empty($arr['status'])){
				if($arr['status'] == 'del'){
					$this->success('删除成功',url('index').$url,'',1);
				}else if($arr['status'] == 'sort'){
					$this->success('排序成功',url('index').$url,'',1);
				}
			}else if($arr['status'] === 0){
				$this->redirect('index').$url;
			}else{
				$data = $arr['arr'];
				$testIds = $arr['testIds'];
				$type_data = Db::table('type')->field('type_id,type_name')->where('nav_id',$datas['nav_id'])->order('type_sort desc,type_id asc')->select();
				$s_data = Db::table('specific')->where('type_id',$datas['type_id'])->field('s_id,s_name,s_show')->order('s_sort desc,s_id asc')->select();
			}
		}else{
			$where = [];
			if($admin['role_id'] != 1) $where['c.nav_id'] = ['in',$admin['nav_id']];
			$data = CourseModel::getAllData($where, ['status'=>'select']);
			$type_data = Db::table('type')->field('type_id,type_name')->order('type_sort desc,type_id asc')->select();
			$s_data = Db::table('specific')->field('s_id,s_name,s_show')->order('s_sort desc,s_id asc')->select();
		}
		if($admin['role_id'] != 1) $wherenav['nav_id'] = ['in',$admin['nav_id']];
		$nav_data = Db::table('nav')->field('nav_id,nav_name')->where($wherenav)->order('nav_sort desc')->select();
		$this->assign('s_data',$s_data);
		$this->assign('nav_data',$nav_data);
		$this->assign('type_data',$type_data);
		$this->assign('data',$data);
		$this->assign('param',$datas);
		return $this->fetch('index');
		
	}

	
	/**
	 * [typeedit 课程分类修改页面]
	 * @return [type] [description]
	 */
	public function edit(){
		$request = Request::instance();
		$param = $request->param();
		$course_data = Db::table('course')
				->where('course_id',$param['id'])
				->find();
		if(input('post.')){
			$data = input('post.');
			if ( $data['uploads'] == 0 && (!$course_data || ($course_data && $data['vid'] != $course_data['vid'])) )
			{
				$ccdata = [
					'userid' => '2CF6808DAEF7DA69',
					'format' => 'json',
					'videoid' => $param['vid'],
				];
				list($result, $status, $message) = ccTHQS($ccdata, 'https://spark.bokecc.com/api/video/v6');
				if ($status == 'OK') $data['video_time'] = $result['video']['duration'];
			}
			unset($data['uploads']);
			unset($data['test-editormd-html-code']);
			$data['updated_time'] = date('Y-m-d H:i:s',time());
			$data['updated_admin_id'] = $this->admin['admin_id'];
			$data['course_name'] = trim($data['course_name']);
			if ($data['type'] == 1)
			{
				$ccData = [
					'userid' => '2CF6808DAEF7DA69',
					'format' => 'json',
					'videoid' => $param['vid'],
				];
				list($result, $status, $message) = ccTHQS($ccData, 'http://spark.bokecc.com/api/video/v6');
				if ($status == 'OK') $data['video_time'] = $result['video']['duration'];

			}
			if ($param['id']){
				$variable = Db::table('course')->where('course_id',$param['id'])->update($data);
			}else{
				$data['created_time'] = $data['updated_time'];
				$data['created_admin_id'] = $data['updated_admin_id'];
				$variable = Db::table('course')->insert($data);
			}
			if($variable){
				$url = '?status=select';
				if(input('post.nav_id')) $url .= '&nav_id='.input('post.nav_id');
				if(input('post.type_id')) $url .= '&type_id='.input('post.type_id');
				if(input('post.s_id')) $url .= '&s_id='.input('post.s_id');
				$this->success('编辑成功',url('index').$url,'',1);
			}else{
				$this->error('编辑失败');
			}
		}else{
			$nav_data = Db::table('nav')->field('nav_id,nav_name')->select();
			$type_data = Db::table('type')->field('type_id,type_name')->where(['nav_id'=>$course_data['nav_id']])->order('type_sort desc')->select();
			$s_data = Db::table('specific')->field('s_id,s_name')->where(['type_id'=>$course_data['type_id']])->order('s_sort desc')->select();
			return $this->fetch('edit',[
				'nav_data' => $nav_data,
				'course_data' => $course_data,
				'type_data' => $type_data,
				's_data' => $s_data,
			]);
		}
	}


	/**
	 * [typedelete 课程导航删除]
	 * @return [type] [description]
	 */
	public function delete(){
		$request = Request::instance();
		$course_id = $request->param('course_id');
		Db::table('know_user_log')->where('course_id',$course_id)->delete();
		Db::table('know_user_record')->where('course_id',$course_id)->delete();
		$del = Db::table('course')->where('course_id',$course_id)->delete();
		if($del){
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败');
		}
	}



	/**
	 * [getType 获取到课程分类]
	 * @return [type] [description]
	 */
	public function getType(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		$type_data = Db::table('type')->field('type_id,type_name')->where('nav_id',$nav_id)->order('type_sort desc,type_id asc')->select();
		$data = array(
			'type_data' => $type_data,	
		);
		echo json_encode($data);
	}

	/**
	 * [getS 获取到课程具体分类]
	 * @return [type] [description]
	 */
	public function getS(){
		$request = Request::instance();
		$type_id = $request->param('type_id');
		$s_data = Db::table('specific')->field('s_id,s_name')->where('type_id',$type_id)->order('s_sort desc,s_id asc')->select();
		$course_data = Db::table('course')->field('course_sort,course_id')->where('type_id',$type_id)->order('course_sort asc')->find();
		if($course_data){
			$sort = $course_data['course_sort'] - 5;
		}else{
			$sort = 999999;
		}
		$data = array(
			's_data' => $s_data,
			'sort' => $sort
		);
		echo json_encode($data);
	}


	public function uploads(Request $request)
	{
		$data = $request->param();
		$data['userid'] = '2CF6808DAEF7DA69';
		$data['notify_url'] = uris().'/index/other/ccnotify';
		unset($data['_token']);
		list($result, $status, $message) = ccTHQS($data, 'http://spark.bokecc.com/api/video/create/v2');
		return json([
			'result' => $result,
			'status' => $status,
			'message' => $message
		]);

	}

	public function category(Request $request)
	{
		$data = [
			'userid' => '2CF6808DAEF7DA69',
			'format' => 'json',
		];
		list($result, $status, $message) = ccTHQS($data, 'http://spark.bokecc.com/api/video/category/v2');
		return json([
			'result' => $result,
			'status' => $status,
			'message' => $message
		]);
	}


}